#include "MeterData.h"

#include <float.h>
#include <limits.h>

#include "ComDefine.h"

bool CMeterData::IsCommNormal()
{
    return (0 == m_uCommState);
}

bool CMeterData::IsCommFault()
{
    return (1 == m_uCommState);
}

bool CMeterData::IsRunning()
{
    return (0 == m_uRunState);
}

bool CMeterData::IsStopped()
{
    return (1 == m_uRunState);
}

bool CMeterData::IsFault()
{
    return (1 == m_uFaultState);
}

bool CMeterData::IsNoFault()
{
    return (0 == m_uFaultState);
}

bool CMeterData::IsAlarm()
{
    return (1 == m_uAlarmState);
}

bool CMeterData::IsNoAlarm()
{
    return (0 == m_uAlarmState);
}

bool CMeterData::IsOpening()
{
    return (1 == m_uSwitchState);
}

bool CMeterData::IsClosed()
{
    return (0 == m_uSwitchState);
}

//获取***的显示文本
QString CMeterData::GetCommStateText()
{
    if (0xFFFF == m_uCommState)
    {
        return QObject::tr("---");
    }
    else if (1 == m_uCommState)
    {
        return QObject::tr("故障");
    }
    else
    {
        return QObject::tr("正常");
    }
}

void CMeterData::InitToInvalid()
{
    m_uCommState = 0xFFFF;
    for (int i = 0; i < ARRAYSIZE(m_uRev); ++i)
    {
        m_uRev[i] = 0xFFFF;
    }
    for (int i = 0; i < ARRAYSIZE(m_fRev); ++i)
    {
        m_fRev[i] = FLT_MAX;
    }

    //转发的100个UINT16数据
    m_uRunState = 0xFFFF;
    m_uFaultState = 0xFFFF;
    m_uAlarmState = 0xFFFF;
    m_uSwitchState = 0xFFFF;

    m_uVolRatio = 0xFFFF;
    m_uCurRatio = 0xFFFF;
    m_uDevCode1 = 0xFFFF;
    m_uDevCode2 = 0xFFFF;
    m_uDevCode3 = 0xFFFF;
    //举例：若表号为123456789012，则编号1为12,编号2为34,编号3为56,编号4为78,编号5为90,编号6为12
    m_uDevCode4 = 0xFFFF;
    m_uDevCode5 = 0xFFFF;
    m_uDevCode6 = 0xFFFF;
    m_uFuncType = 0xFFFF;
    for (int i = 0; i < ARRAYSIZE(m_uData); ++i)
    {
        m_uData[i] = 0xFFFF;
    }

    //转发的100个FLOAT数据
    m_fCurFactor = FLT_MAX;
    m_fVolFactor = FLT_MAX;
    m_fPosActPowerUpLimit = FLT_MAX;
    m_fNegActPowerUpLimit = FLT_MAX;

    //相电压
    m_fAPhaseVol = FLT_MAX;
    m_fBPhaseVol = FLT_MAX;
    m_fCPhaseVol = FLT_MAX;
    m_fTotalPhaseVol = FLT_MAX;

    //线电压
    m_fABLineVol = FLT_MAX;
    m_fBCLineVol = FLT_MAX;
    m_fCALineVol = FLT_MAX;
    m_fTotalLineVol = FLT_MAX;

    //电流
    m_fAPhaseCur = FLT_MAX;
    m_fBPhaseCur = FLT_MAX;
    m_fCPhaseCur = FLT_MAX;
    m_fNeutralLineCur = FLT_MAX;
    m_fTotalPhaseCur = FLT_MAX;

    //有功功率ActivePower
    m_fAPhaseActPower = FLT_MAX;
    m_fBPhaseActPower = FLT_MAX;
    m_fCPhaseActPower = FLT_MAX;
    m_fTotalActPower = FLT_MAX;

    //无功功率ReactivePower
    m_fAPhaseReactPower = FLT_MAX;
    m_fBPhaseReactPower = FLT_MAX;
    m_fCPhaseReactPower = FLT_MAX;
    m_fTotalReactPower = FLT_MAX;

    //视在功率ApparentPower
    m_fAPhaseApprPower = FLT_MAX;
    m_fBPhaseApprPower = FLT_MAX;
    m_fCPhaseApprPower = FLT_MAX;
    m_fTotalApprPower = FLT_MAX;

    //功率因素PowerFactor
    m_fAPhasePowerFactor = FLT_MAX;
    m_fBPhasePowerFactor = FLT_MAX;
    m_fCPhasePowerFactor = FLT_MAX;
    m_fTotalPowerFactor = FLT_MAX;

    //频率
    m_fAPhaseFreq = FLT_MAX;
    m_fBPhaseFreq = FLT_MAX;
    m_fCPhaseFreq = FLT_MAX;
    m_fTotalFreq = FLT_MAX;

    //正向有功电能
    m_fAPhasePosActPowerEq = FLT_MAX;
    m_fBPhasePosActPowerEq = FLT_MAX;
    m_fCPhasePosActPowerEq = FLT_MAX;
    m_fTotalPosActPowerEq = FLT_MAX;
    m_fSharpPosActPowerEq = FLT_MAX;
    m_fPeakPosActPowerEq = FLT_MAX;
    m_fFlatPosActPowerEq = FLT_MAX;
    m_fOpeakPosActPowerEq = FLT_MAX;

    //组合有功电能
    m_fTotalComActPowerEq = FLT_MAX;
    m_fSharpComActPowerEq = FLT_MAX;
    m_fPeakComActPowerEq = FLT_MAX;
    m_fFlatComActPowerEq = FLT_MAX;
    m_fOpeakComActPowerEq = FLT_MAX;

    //反向有功电能
    m_fTotalNegActPowerEq = FLT_MAX;
    m_fSharpNegActPowerEq = FLT_MAX;
    m_fPeakNegActPowerEq = FLT_MAX;
    m_fFlatNegActPowerEq = FLT_MAX;
    m_fOpeakNegActPowerEq = FLT_MAX;

    //无功电能
    m_fTotalPosReactPowerEq = FLT_MAX;
    m_fTotalNegReactPowerEq = FLT_MAX;
    m_fTotalComReactPowerEq = FLT_MAX;
    m_fSharpComReactPowerEq = FLT_MAX;
    m_fPeakComReactPowerEq = FLT_MAX;
    m_fFlatComReactPowerEq = FLT_MAX;
    m_fOpeakComReactPowerEq = FLT_MAX;

    //总电量
    m_fTotalChgEq = FLT_MAX;
    m_fTotalDisChgEq = FLT_MAX;

    //日电量
    m_fCurDayZeroChgEq = FLT_MAX;
    m_fCurDayZeroDisChgEq = FLT_MAX;
    m_fCurDayChgEq = FLT_MAX;
    m_fCurDayDisChgEq = FLT_MAX;
    m_fLastDayChgEq = FLT_MAX;
    m_fLastDayDisChgEq = FLT_MAX;

    //周电量
    m_fCurWeekZeroChgEq = FLT_MAX;
    m_fCurWeekZeroDisChgEq = FLT_MAX;
    m_fCurWeekChgEq = FLT_MAX;
    m_fCurWeekDisChgEq = FLT_MAX;
    m_fLastWeekChgEq = FLT_MAX;
    m_fLastWeekDisChgEq = FLT_MAX;
    //月电量
    m_fCurMonthZeroChgEq = FLT_MAX;
    m_fCurMonthZeroDisChgEq = FLT_MAX;
    m_fCurMonthChgEq = FLT_MAX;
    m_fCurMonthDisChgEq = FLT_MAX;
    m_fLastMonthChgEq = FLT_MAX;
    m_fLastMonthDisChgEq = FLT_MAX;
    //年电量
    m_fCurYearZeroChgEq = FLT_MAX;
    m_fCurYearZeroDisChgEq = FLT_MAX;
    m_fCurYearChgEq = FLT_MAX;
    m_fCurYearDisChgEq = FLT_MAX;
    m_fLastYearChgEq = FLT_MAX;
    m_fLastYearDisChgEq = FLT_MAX;

    m_fPluseConstant = FLT_MAX;
    m_fVolUnbalance = FLT_MAX;
    m_fCurUnbalance = FLT_MAX;

    for (int i = 0; i < ARRAYSIZE(m_fData); ++i)
    {
        m_fData[i] = FLT_MAX;
    }

    for (int i = 0; i < ARRAYSIZE(m_uSetData); ++i)
    {
        m_uSetData[i] = 0xFFFF;
    }

    for (int i = 0; i < ARRAYSIZE(m_uSetDataCopy); ++i)
    {
        m_uSetDataCopy[i] = 0xFFFF;
    }
}

void CMeterData::InitToZero()
{
    m_uCommState = 0;
    for (int i = 0; i < ARRAYSIZE(m_uRev); ++i)
    {
        m_uRev[i] = 0;
    }
    for (int i = 0; i < ARRAYSIZE(m_fRev); ++i)
    {
        m_fRev[i] = 0.0;
    }

    //转发的100个UINT16数据
    m_uRunState = 0;
    m_uFaultState = 0;
    m_uAlarmState = 0;
    m_uSwitchState = 0;

    m_uVolRatio = 0;
    m_uCurRatio = 0;
    m_uDevCode1 = 0;
    m_uDevCode2 = 0;
    m_uDevCode3 = 0;
    //举例：若表号为123456789012，则编号1为12,编号2为34,编号3为56,编号4为78,编号5为90,编号6为12
    m_uDevCode4 = 0;
    m_uDevCode5 = 0;
    m_uDevCode6 = 0;
    m_uFuncType = 0;
    for (int i = 0; i < ARRAYSIZE(m_uData); ++i)
    {
        m_uData[i] = 0;
    }

    //转发的100个FLOAT数据
    m_fCurFactor = 0.0;
    m_fVolFactor = 0.0;
    m_fPosActPowerUpLimit = 0.0;
    m_fNegActPowerUpLimit = 0.0;

    //相电压
    m_fAPhaseVol = 0.0;
    m_fBPhaseVol = 0.0;
    m_fCPhaseVol = 0.0;
    m_fTotalPhaseVol = 0.0;

    //线电压
    m_fABLineVol = 0.0;
    m_fBCLineVol = 0.0;
    m_fCALineVol = 0.0;
    m_fTotalLineVol = 0.0;

    //电流
    m_fAPhaseCur = 0.0;
    m_fBPhaseCur = 0.0;
    m_fCPhaseCur = 0.0;
    m_fNeutralLineCur = 0.0;
    m_fTotalPhaseCur = 0.0;

    //有功功率ActivePower
    m_fAPhaseActPower = 0.0;
    m_fBPhaseActPower = 0.0;
    m_fCPhaseActPower = 0.0;
    m_fTotalActPower = 0.0;

    //无功功率ReactivePower
    m_fAPhaseReactPower = 0.0;
    m_fBPhaseReactPower = 0.0;
    m_fCPhaseReactPower = 0.0;
    m_fTotalReactPower = 0.0;

    //视在功率ApparentPower
    m_fAPhaseApprPower = 0.0;
    m_fBPhaseApprPower = 0.0;
    m_fCPhaseApprPower = 0.0;
    m_fTotalApprPower = 0.0;

    //功率因素PowerFactor
    m_fAPhasePowerFactor = 0.0;
    m_fBPhasePowerFactor = 0.0;
    m_fCPhasePowerFactor = 0.0;
    m_fTotalPowerFactor = 0.0;

    //频率
    m_fAPhaseFreq = 0.0;
    m_fBPhaseFreq = 0.0;
    m_fCPhaseFreq = 0.0;
    m_fTotalFreq = 0.0;

    //正向有功电能
    m_fAPhasePosActPowerEq = 0.0;
    m_fBPhasePosActPowerEq = 0.0;
    m_fCPhasePosActPowerEq = 0.0;
    m_fTotalPosActPowerEq = 0.0;
    m_fSharpPosActPowerEq = 0.0;
    m_fPeakPosActPowerEq = 0.0;
    m_fFlatPosActPowerEq = 0.0;
    m_fOpeakPosActPowerEq = 0.0;

    //组合有功电能
    m_fTotalComActPowerEq = 0.0;
    m_fSharpComActPowerEq = 0.0;
    m_fPeakComActPowerEq = 0.0;
    m_fFlatComActPowerEq = 0.0;
    m_fOpeakComActPowerEq = 0.0;

    //反向有功电能
    m_fTotalNegActPowerEq = 0.0;
    m_fSharpNegActPowerEq = 0.0;
    m_fPeakNegActPowerEq = 0.0;
    m_fFlatNegActPowerEq = 0.0;
    m_fOpeakNegActPowerEq = 0.0;

    //无功电能
    m_fTotalPosReactPowerEq = 0.0;
    m_fTotalNegReactPowerEq = 0.0;
    m_fTotalComReactPowerEq = 0.0;
    m_fSharpComReactPowerEq = 0.0;
    m_fPeakComReactPowerEq = 0.0;
    m_fFlatComReactPowerEq = 0.0;
    m_fOpeakComReactPowerEq = 0.0;

    //总电量
    m_fTotalChgEq = 0.0;
    m_fTotalDisChgEq = 0.0;

    //日电量
    m_fCurDayZeroChgEq = 0.0;
    m_fCurDayZeroDisChgEq = 0.0;
    m_fCurDayChgEq = 0.0;
    m_fCurDayDisChgEq = 0.0;
    m_fLastDayChgEq = 0.0;
    m_fLastDayDisChgEq = 0.0;

    //周电量
    m_fCurWeekZeroChgEq = 0.0;
    m_fCurWeekZeroDisChgEq = 0.0;
    m_fCurWeekChgEq = 0.0;
    m_fCurWeekDisChgEq = 0.0;
    m_fLastWeekChgEq = 0.0;
    m_fLastWeekDisChgEq = 0.0;
    //月电量
    m_fCurMonthZeroChgEq = 0.0;
    m_fCurMonthZeroDisChgEq = 0.0;
    m_fCurMonthChgEq = 0.0;
    m_fCurMonthDisChgEq = 0.0;
    m_fLastMonthChgEq = 0.0;
    m_fLastMonthDisChgEq = 0.0;
    //年电量
    m_fCurYearZeroChgEq = 0.0;
    m_fCurYearZeroDisChgEq = 0.0;
    m_fCurYearChgEq = 0.0;
    m_fCurYearDisChgEq = 0.0;
    m_fLastYearChgEq = 0.0;
    m_fLastYearDisChgEq = 0.0;

    m_fPluseConstant = 0.0;
    m_fVolUnbalance = 0.0;
    m_fCurUnbalance = 0.0;

    for (int i = 0; i < ARRAYSIZE(m_fData); ++i)
    {
        m_fData[i] = 0.0;
    }

    for (int i = 0; i < ARRAYSIZE(m_uSetData); ++i)
    {
        m_uSetData[i] = 0;
    }

    for (int i = 0; i < ARRAYSIZE(m_uSetDataCopy); ++i)
    {
        m_uSetDataCopy[i] = 0;
    }
}

void CMeterData::InitToValue(quint16 uvalue, float fvalue)
{
    m_uCommState = uvalue;
    for (int i = 0; i < ARRAYSIZE(m_uRev); ++i)
    {
        m_uRev[i] = uvalue;
    }
    for (int i = 0; i < ARRAYSIZE(m_fRev); ++i)
    {
        m_fRev[i] = fvalue;
    }

    //转发的100个UINT16数据
    m_uRunState = uvalue;
    m_uFaultState = uvalue;
    m_uAlarmState = uvalue;
    m_uSwitchState = uvalue;

    m_uVolRatio = uvalue;
    m_uCurRatio = uvalue;
    m_uDevCode1 = uvalue;
    m_uDevCode2 = uvalue;
    m_uDevCode3 = uvalue;
    //举例：若表号为123456789012，则编号1为12,编号2为34,编号3为56,编号4为78,编号5为90,编号6为12
    m_uDevCode4 = uvalue;
    m_uDevCode5 = uvalue;
    m_uDevCode6 = uvalue;
    m_uFuncType = uvalue;
    for (int i = 0; i < ARRAYSIZE(m_uData); ++i)
    {
        m_uData[i] = uvalue;
    }

    //转发的100个FLOAT数据
    m_fCurFactor = fvalue;
    m_fVolFactor = fvalue;
    m_fPosActPowerUpLimit = fvalue;
    m_fNegActPowerUpLimit = fvalue;

    //相电压
    m_fAPhaseVol = fvalue;
    m_fBPhaseVol = fvalue;
    m_fCPhaseVol = fvalue;
    m_fTotalPhaseVol = fvalue;

    //线电压
    m_fABLineVol = fvalue;
    m_fBCLineVol = fvalue;
    m_fCALineVol = fvalue;
    m_fTotalLineVol = fvalue;

    //电流
    m_fAPhaseCur = fvalue;
    m_fBPhaseCur = fvalue;
    m_fCPhaseCur = fvalue;
    m_fNeutralLineCur = fvalue;
    m_fTotalPhaseCur = fvalue;

    //有功功率ActivePower
    m_fAPhaseActPower = fvalue;
    m_fBPhaseActPower = fvalue;
    m_fCPhaseActPower = fvalue;
    m_fTotalActPower = fvalue;

    //无功功率ReactivePower
    m_fAPhaseReactPower = fvalue;
    m_fBPhaseReactPower = fvalue;
    m_fCPhaseReactPower = fvalue;
    m_fTotalReactPower = fvalue;

    //视在功率ApparentPower
    m_fAPhaseApprPower = fvalue;
    m_fBPhaseApprPower = fvalue;
    m_fCPhaseApprPower = fvalue;
    m_fTotalApprPower = fvalue;

    //功率因素PowerFactor
    m_fAPhasePowerFactor = fvalue;
    m_fBPhasePowerFactor = fvalue;
    m_fCPhasePowerFactor = fvalue;
    m_fTotalPowerFactor = fvalue;

    //频率
    m_fAPhaseFreq = fvalue;
    m_fBPhaseFreq = fvalue;
    m_fCPhaseFreq = fvalue;
    m_fTotalFreq = fvalue;

    //正向有功电能
    m_fAPhasePosActPowerEq = fvalue;
    m_fBPhasePosActPowerEq = fvalue;
    m_fCPhasePosActPowerEq = fvalue;
    m_fTotalPosActPowerEq = fvalue;
    m_fSharpPosActPowerEq = fvalue;
    m_fPeakPosActPowerEq = fvalue;
    m_fFlatPosActPowerEq = fvalue;
    m_fOpeakPosActPowerEq = fvalue;

    //组合有功电能
    m_fTotalComActPowerEq = fvalue;
    m_fSharpComActPowerEq = fvalue;
    m_fPeakComActPowerEq = fvalue;
    m_fFlatComActPowerEq = fvalue;
    m_fOpeakComActPowerEq = fvalue;

    //反向有功电能
    m_fTotalNegActPowerEq = fvalue;
    m_fSharpNegActPowerEq = fvalue;
    m_fPeakNegActPowerEq = fvalue;
    m_fFlatNegActPowerEq = fvalue;
    m_fOpeakNegActPowerEq = fvalue;

    //无功电能
    m_fTotalPosReactPowerEq = fvalue;
    m_fTotalNegReactPowerEq = fvalue;
    m_fTotalComReactPowerEq = fvalue;
    m_fSharpComReactPowerEq = fvalue;
    m_fPeakComReactPowerEq = fvalue;
    m_fFlatComReactPowerEq = fvalue;
    m_fOpeakComReactPowerEq = fvalue;

    //总电量
    m_fTotalChgEq = fvalue;
    m_fTotalDisChgEq = fvalue;

    //日电量
    m_fCurDayZeroChgEq = fvalue;
    m_fCurDayZeroDisChgEq = fvalue;
    m_fCurDayChgEq = fvalue;
    m_fCurDayDisChgEq = fvalue;
    m_fLastDayChgEq = fvalue;
    m_fLastDayDisChgEq = fvalue;

    //周电量
    m_fCurWeekZeroChgEq = fvalue;
    m_fCurWeekZeroDisChgEq = fvalue;
    m_fCurWeekChgEq = fvalue;
    m_fCurWeekDisChgEq = fvalue;
    m_fLastWeekChgEq = fvalue;
    m_fLastWeekDisChgEq = fvalue;
    //月电量
    m_fCurMonthZeroChgEq = fvalue;
    m_fCurMonthZeroDisChgEq = fvalue;
    m_fCurMonthChgEq = fvalue;
    m_fCurMonthDisChgEq = fvalue;
    m_fLastMonthChgEq = fvalue;
    m_fLastMonthDisChgEq = fvalue;
    //年电量
    m_fCurYearZeroChgEq = fvalue;
    m_fCurYearZeroDisChgEq = fvalue;
    m_fCurYearChgEq = fvalue;
    m_fCurYearDisChgEq = fvalue;
    m_fLastYearChgEq = fvalue;
    m_fLastYearDisChgEq = fvalue;

    m_fPluseConstant = fvalue;
    m_fVolUnbalance = fvalue;
    m_fCurUnbalance = fvalue;

    for (int i = 0; i < ARRAYSIZE(m_fData); ++i)
    {
        m_fData[i] = fvalue;
    }

    for (int i = 0; i < ARRAYSIZE(m_uSetData); ++i)
    {
        m_uSetData[i] = uvalue;
    }

    for (int i = 0; i < ARRAYSIZE(m_uSetDataCopy); ++i)
    {
        m_uSetDataCopy[i] = uvalue;
    }
}
